{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy中的arg运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   0.,    1.,    2., ...,  998.,  999., 1000.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "x=np.linspace(0,1000,1001)\n",
    "x\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最大值最小值索引：argmax, argmin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(1000.0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.max()\n",
    "x.argmax() #返回最大值所在的索引\n",
    "x[x.argmax()] #返回最大值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2, 1, 8, 5, 7, 6, 0, 4, 9])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.arange(10)\n",
    "np.random.shuffle(a)\n",
    "a\n",
    "np.argsort(a) #返回排序后索引的数组\n",
    "np.argsort(-a) #返回排序后,元素的索引 数组\n",
    "a\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "patition 分区:用于对数组进行部分排序，它会将数组分成多个部分，使得其中一部分按升序排序，而另一部分不一定完全按顺序排列。具体来说，np.partition() 会返回一个数组，使得数组的前 k 个元素（根据给定的 k）被排序，而其他部分保持不变。\n",
    "\n",
    "与 argsort() 不同，partition() 仅仅对数组的某个部分进行排序，而不是对整个数组排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 5, 6, 9, 2, 7, 3, 8, 4])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=np.arange(10)\n",
    "np.random.shuffle(b)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 5, 7, 9, 2, 3, 6, 8, 4])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.partition(b,3)\n",
    "np.argpartition(b,3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "machineLearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
